/****************************** (C) COPYRIGHT 2016 RSEStudio ****************************
* File Name          : 
* Author             : RSES_Liyang
* Version            : V1.0.0
* Date               : 00-May-18
* Description        : 
*****************************************************************************************/

#ifndef __W25X_H__
#define __W25X_H__

/* Includes ----------------------------------------------------------------------------*/

/* Exported types ----------------------------------------------------------------------*/
/* Exported constants ------------------------------------------------------------------*/
/* Exported macro ----------------------------------------------------------------------*/


/* Exported functions ----------------------------------------------------------------- */

typedef struct 
{
	uint8_t  id;
	uint8_t  Used;
	uint16_t group;
	uint32_t Start_Date;
}file_info_simple_t;

extern file_info_simple_t file_info_simple[];


/* EasyFlash error code */
typedef enum {
    EF_NO_ERR,
    EF_ERASE_ERR,
    EF_READ_ERR,
    EF_WRITE_ERR,
    EF_ENV_NAME_ERR,
    EF_ENV_NAME_EXIST,
    EF_ENV_FULL,
    EF_ENV_INIT_FAILED,
} EfErrCode;

//typedef struct _meas_head
//{
//	uint16_t 	group;
//	uint16_t	SampleInterval;
//	uint16_t 	Status;	
//	uint16_t	TagID;
//		
//	uint32_t 	Start_Cond;
//	uint32_t	Start_Params_1;
//	uint32_t	Start_Params_2;
//	uint32_t	Start_Params_3;
//	uint32_t	Start_Params_4;
//	
//	uint32_t 	Stop_Cond;
//	uint32_t	Stop_Params_1;
//	uint32_t	Stop_Params_2;
//	uint32_t	Stop_Params_3;
//	uint32_t	Stop_Params_4;
//}meas_head_t;



typedef struct _file_info file_info_t;

struct _file_info
{
	uint16_t 	file_id;
	uint16_t	StartStatus;
	uint16_t 	EndStatus;
    uint16_t 	dummy1;
	uint32_t 	Start_Date;
	uint32_t 	End_Date;
	uint32_t 	StartPos;
	uint32_t	End_Pos;	
	uint32_t 	TotalItem;
	//uint32_t 	Addr;
	uint32_t 	Write_Addr;
	uint32_t  	Current_addr;
    
    uint8_t     v_data_point;
    uint8_t     dummy2;   
	uint16_t	Used;
    
    uint16_t 	group;
	uint16_t 	Status;	
	uint16_t	TagID;
    //uint16_t    SampleInterval;
    
    MEAS_PARAMS Meas_head;
};



uint8_t SerialFLASH_initialize(void);

uint8_t SerialFLASH_Disk_Status(void);

void W25X_Reset(void);

void W25X_Read_Sector(uint32_t nSector, uint8_t* pBuffer,uint16_t byte);
void W25X_Read_Sector_offset(uint32_t nSector,uint16_t offset, uint8_t* pBuffer,uint16_t byte);

void W25X_Erase_Sector(uint32_t nDest);

void W25X_Write_Sector(uint32_t nSector, uint8_t* pBuffer,uint16_t Page);

void W25X_Write_Enable(void);

void W25X_Wait_Busy(void);

uint8_t W25X_Read_StatusReg(void);

uint8_t ReadDeviceID(void);

void ReadUniqueID(uint8_t *UniqueID);

uint8_t W25X_CS_Status(void);

uint8_t ChipSHDN(void);

uint8_t ChipPWUP(void);


void W25X_Read_Addr(uint32_t Addr, uint8_t* pBuffer,uint16_t Size);
void W25X_Erase_Sector(uint32_t Addr);
void W25X_WriteSector_Addr(uint32_t Addr, uint8_t* pBuffer,int32_t Size);

void Mem_FileHead_Init(void);
void Mem_FileHead_ReInit(void);
file_info_t *Mem_File_id_to_head(uint16_t id);
file_info_t *Mem_File_CreateRecoder(uint16_t group,uint16_t tag_id,uint32_t start_date,uint16_t start_status,MEAS_PARAMS *pmeas_params);
int32_t Mem_File_CloseRecoder(file_info_t *file_head,uint16_t end_status);
int32_t Mem_File_WriteItem(file_info_t *file_head,uint8_t res);
//int32_t Mem_File_GetGroupFileCounter(uint16_t group);
int32_t Mem_File_GetGroupFile(uint16_t group,int16_t file_id,file_info_t **file_head);
int32_t Mem_File_ReadFileInit(file_info_t *file_head);
int32_t Mem_File_ReadFile(file_info_t *file_head,int offset,int ReadLength,uint8_t *pbuf);
int32_t Mem_File_Remove(uint16_t group,int16_t file_id);
void Mem_MeasParams_Load(void);
void Mem_MeasParams_Save(void);
void Mem_device_config_Load(void);
void Mem_device_config_save(void);
void nvm_thermo_calib_Load(void);
void nvm_thermo_calib_save(void);
void nvm_calib_pressure_Load(void);
void nvm_calib_pressure_save(void);
void nvm_calib_oxygen_Load(void);
void nvm_calib_oxygen_save(void);
void nvm_calib_angle_Load(void);
void nvm_calib_angle_save(void);
void W25X_Erase_Chip(void);
void erase_meas_info(void);
void erase_all_headinfo(void);
int16_t read_file_info_nocheck(uint16_t id,file_info_t *FileInfo);
uint16_t GetGroupFile_use(void);

void Mem_File_Create_Test_Recoder(void);

int fw_flash_write(uint32_t addr,uint8_t *buf,uint16_t size);

int fw_flash_write_success(int fw_size);
int fw_flash_write_clean(void);

uint8_t *get_dev_mem(void);
void set_dev_mem(uint8_t *p_buf,uint16_t len);

EfErrCode ef_log_clean(void) ;
void log_write(uint8_t lvl,uint8_t tag,int16_t v1,int16_t v2,int32_t v3,uint8_t *pbuf,uint16_t len);
size_t ef_log_get_used_size(void);
EfErrCode ef_log_read(size_t index, uint32_t *log, size_t size);

#endif
/********************************(C)COPYRIGHT 2016 RSEStudio ****************************/
/*****************************************END OF FILE************************************/


